package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Menu;
import com.itheima.service.MenuService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/menu")
public class MenuController {

    @Reference
    private MenuService menuService;
    //menu列表展示
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = menuService.pageQuery(queryPageBean);
        return pageResult;
    }
    //menu添加
    @RequestMapping("/add")
    public Result add(@RequestBody Menu menu){
        try {
            menuService.add(menu);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"添加菜单失败");
        }
        return new Result(true,"添加菜单成功");
    }

    //menu删除
    @RequestMapping("/delete")
    public Result delete(Integer id){
        try {
            menuService.deleteById(id);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"删除菜单失败");
        }
        return new Result(true,"删除菜单成功");
    }
    //menu编辑表数据回显（通过id查询）
    @RequestMapping("/findById")
    public Result findById(Integer id){
        try {
            Menu menu = menuService.findById(id);
            return new Result(true,"数据查询成功",menu);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"数据查询失败");
        }
    }
    //menu编辑
    @RequestMapping("/edit")
    public Result edit(@RequestBody Menu menu){

        System.out.println(menu.getName());
        System.out.println(menu.getLinkUrl());
        System.out.println(menu.getPath());
        System.out.println(menu.getPriority());
        System.out.println(menu.getIcon());
        System.out.println(menu.getDescription());
        System.out.println(menu.getParentMenuId());
        System.out.println(menu.getLevel());

         try {
             menuService.edit(menu);
         } catch (Exception e) {
             e.printStackTrace();
             return new Result(false,"编辑菜单失败");
         }
         return new Result(true,"编辑菜单成功");
    }
    //获取下拉菜单
    @RequestMapping("/findParentMenu")
    public Result findParentMenu(){
        List<Menu> parentMenuList = menuService.findParentMenu();
        return new Result(true,"查询上级菜单成功",parentMenuList);
    }
    //菜单动态展示成功
    @RequestMapping("/findMenuList")
    public Result findMenuList(){
        List<Menu> MenuList = menuService.findMenuList();
        return new Result(true,"菜单动态展示成功",MenuList);
    }

}
